package kmi;

import static org.junit.Assert.assertTrue;
import robocode.control.snapshot.IRobotSnapshot;
import robocode.control.testing.RobotTestBed;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;

/**
 * Illustrates JUnit testing of Robocode robots. This test simply verifies that GreenMachine backs
 * up after hitting a wall.
 * 
 * @author Mitchell Kupfer
 */
public class TestHitWall extends RobotTestBed {
  IRobotSnapshot wallHitter = null;
  boolean backedUp = false;

  /**
   * Match between GreenMachine and Crazy.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.Crazy,kmi.GreenMachine";
  }

  /**
   * This test runs for 20 rounds.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 20;
  }

  /**
   * see if we hit a wall, and if we backed up.
   * 
   * @param event Info about the current state of the battle.
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {

    // Get the snapshot of the robot GreenMachine
    IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1];

    if (robot.getState().isHitWall()) {
      wallHitter = robot;
    }

    // if it has hit a wall
    else if (wallHitter != null) {

      // if it hit left side, and is going right
      if (wallHitter.getX() < 40 && robot.getX() > wallHitter.getX()) {
        backedUp = true;
        wallHitter = null;
        return;
      }

      // if it hit right side and is going left
      if (wallHitter.getX() > width - 40 && robot.getX() < wallHitter.getX()) {
        backedUp = true;
        wallHitter = null;
        return;
      }

      // if it hit bottom and is going up
      if (wallHitter.getY() < 40 && robot.getY() > wallHitter.getY()) {
        backedUp = true;
        wallHitter = null;
        return;
      }

      // if it hit top and is going down
      if (wallHitter.getY() > height - 40 && robot.getY() < wallHitter.getY()) {
        backedUp = true;
        wallHitter = null;
        return;
      }
      
    }

  }

  /**
   * After the battle, check to see that we fired a red bullet the same number of times we were hit.
   * 
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    assertTrue("Robot backs up", backedUp);

  }

}
